package com.coolwen.spring.aop.base;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author CoolWen
 * @version 2020-03-19 9:29
 */
public class PerformProxy implements InvocationHandler {
    //代理对象可以代理任意目标
    private Object target;

    public PerformProxy() {
    }

    public static Object getInstace(Object o) {
        PerformProxy proxy = new PerformProxy();
        proxy.target = o;
        Object result = Proxy.newProxyInstance(o.getClass().getClassLoader(), o.getClass().getInterfaces(), proxy);
        return result;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object rel = null;
//        if (method.isAnnotationPresent(PerformCheck.class)) {
            long start = System.currentTimeMillis();
            rel = method.invoke(target, args);
            long end = System.currentTimeMillis();
            System.out.println(method.getName() + "所需要的时间" + (end - start));
//        } else {
//            rel = method.invoke(target, args);
//        }
        return rel;
    }
}
